package com.example.fragment;


import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.adapter.ListViewAdapter;
import com.example.beans.PaperInformation;
import com.example.inter.PaperInfo;
import com.example.inter.PaperInfoCallBack;
import com.example.myapplication.R;
import com.example.util.PaperInfoforUser;
import com.example.util.UserInfoStudent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentPaperInfo extends Fragment implements PaperInfoCallBack {
    private ListView listView;
    private List<PaperInformation> list = new ArrayList<>();
    private ListViewAdapter listViewAdapter;
    private PaperInformation paperInformation;
    private PaperInfoforUser paperInfoforUser;
    public FragmentPaperInfo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_paper_info,container,false);
        listView = (ListView) view.findViewById(R.id.listView);
        listViewAdapter = new ListViewAdapter(getContext(),list);
        listView.setAdapter(listViewAdapter);
        Bundle bundle = getArguments();
        paperInformation = (PaperInformation) bundle.getSerializable("userinfo");
        paperInfoforUser = new PaperInfoforUser();
        if (paperInformation != null) {
            String userid =paperInformation.getStudent_number();
            paperInfoforUser.getInfo(userid,this);
        }else{
            Snackbar.make(view,"wrong",Snackbar.LENGTH_SHORT).show();
        }

        return view;
    }

    @Override
    public void onPaperInfoCallBackSuccess(String info) {
        Log.i("indo",info);
        List<PaperInformation> mlist = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String paper_name = jsonObject1.getString("paper_name");
                String paper_time = jsonObject1.getString("paper_time");
                if(paper_name.length()>7){
                    paper_name = paper_name.substring(0,7)+"...";
                }
                paperInformation = new PaperInformation();
                paperInformation.setPaper_name(paper_name);
                paperInformation.setPaper_time(paper_time);
                mlist.add(paperInformation);
            }
            listViewAdapter.changeList(mlist);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onFail() {

    }
}
